package com.crm.service.impl;


import com.crm.mapper.IndexMapper;
import com.crm.pojo.dto.IndexDto;
import com.crm.pojo.vo.BusIndexVo;
import com.crm.pojo.vo.IndexListVo;
import com.crm.pojo.vo.IndexNumVo;
import com.crm.pojo.vo.IndexReportvVo;
import com.crm.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.insert.InsertModifierPriority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;

    /**
     * 今日简介
     * @return
     */
    @Override
    public IndexReportvVo getTodayInfo() {

        //今日线索数量
        Integer todayCluesNum = indexMapper.CluesCount();
        //今日商机数量
        Integer todayBusinessName = indexMapper.BusCount();
        //今日合同数量
        Integer todayContractNum = indexMapper.ContractCount();

        IndexReportvVo indexReportvVo = new IndexReportvVo();
        indexReportvVo.setTodayBusinessNum(todayBusinessName);
        indexReportvVo.setTodayCluesNum(todayCluesNum);
        indexReportvVo.setTodayContractNum(todayContractNum);
        return indexReportvVo;
    }

    @Override
    public IndexNumVo getVulnerabilityMap(String beginTimeStr, String endTimeStr) {
        LocalDate beginTime = LocalDate.parse(beginTimeStr);
        LocalDate endTime = LocalDate.parse(endTimeStr);

        Integer CluesNum = indexMapper.CluesCounts(beginTime,endTime);
        Integer BusinessNum = indexMapper.BusCounts(beginTime,endTime);
        Integer ContractNum = indexMapper.ContractCounts(beginTime,endTime);
        Integer effectiveCluesNum = indexMapper.effCluesCount(beginTime,endTime);

        IndexNumVo indexNumVo = new IndexNumVo();
        indexNumVo.setBusinessNums(BusinessNum);
        indexNumVo.setCluesNums(CluesNum);
        indexNumVo.setContractNums(ContractNum);
        indexNumVo.setEffectiveCluesNums(effectiveCluesNum);
        return indexNumVo;
    }

    @Override
    public List<BusIndexVo> getCLueFinish(IndexDto dto) {
        LocalDateTime beginCreateTime =dto.getBeginCreateTime().atTime(0,0,0);
        LocalDateTime endCreateTime =dto.getEndCreateTime().atTime(23,59,59);
        List<BusIndexVo> cLueFinish = indexMapper.getCLueFinish(beginCreateTime, endCreateTime);
        cLueFinish.forEach(d->{
            d.setCreate_by(d.getCreateBy());
        });
        return cLueFinish;
    }

    @Override
    public List<BusIndexVo> getBusinessFinish(IndexDto dto) {
        LocalDateTime beginCreateTime =dto.getBeginCreateTime().atTime(0,0,0);
        LocalDateTime endCreateTime =dto.getEndCreateTime().atTime(23,59,59);
        List<BusIndexVo> businessFinish = indexMapper.getBusinessFinish(beginCreateTime, endCreateTime);
        businessFinish.forEach(d->{
            d.setCreate_by(d.getCreateBy());
        });
        return  businessFinish;
    }

    @Override
    public IndexListVo getIndex() {

//          indexMapper.getTodayNum(dto.getDeptId());
        return IndexListVo.builder()
                .cluesNum(10)
                .businessNum(20)
                .contractNum(30)
                .salesAmount(40.0)
                .todayCluesNum(10)
                .todayBusinessNum(10)
                .todayContractNum(10)
                .todaySalesAmount(20.0)
                .toallocatedCluesNum(0)
                .tofollowedBusinessNum(0)
                .toallocatedBusinessNum(20)
                .tofollowedCluesNum(0)
                .build();
    }

    @Override
    public List<BusIndexVo> getSi() {
        ArrayList<BusIndexVo> list = new ArrayList<>();
        BusIndexVo busIndexVo = new BusIndexVo(null,"zhangsan0","部门0",null,10,86.0);
        BusIndexVo busIndexVo1 = new BusIndexVo(null,"zhangsan1", "部门1",null, 10, 86.0);
        list.add(busIndexVo);
        list.add(busIndexVo1);
        return list;
    }


}
